/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.pay;

import java.io.IOException;
import java.util.Map;

import org.json.JSONObject;

//import com.alipay.api.AlipayApiException;
//import com.alipay.api.AlipayClient;
//import com.alipay.api.DefaultAlipayClient;
//import com.alipay.api.domain.AlipayTradeWapPayModel;
//import com.alipay.api.request.AlipaySystemOauthTokenRequest;
//import com.alipay.api.request.AlipayTradeWapPayRequest;
//import com.alipay.api.request.AlipayUserInfoShareRequest;
//import com.alipay.api.response.AlipaySystemOauthTokenResponse;
//import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.zxy.business.Ope_DB_Cent;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

public class Alipay_now
{
	// 商户订单号，商户网站订单系统中唯一订单号，必填
	public String	WIDout_trade_no	= "";
	// 订单名称，必填
	public String	WIDsubject		= "";
	// 付款金额，必填
	public String	WIDtotal_amount	= "0";
	// 商品描述，可空
	public String	WIDbody			= "";
	// 销售产品码 必填
	public String	product_code	= "xxxxx";
	// 商家支付宝对应ID
	public String	seller_id		= "";

	public String Get_Seller_id()
	{
		Ope_DB_Cent odc = new Ope_DB_Cent();
		seller_id = odc.Get_Seller_ID(WIDout_trade_no);
		return seller_id;
	}

	// 支付宝换取令牌
	@SuppressWarnings("static-access")
	public StringBuilder Ali_Login(String Auth_Code)
	{
		StringBuilder strResult = new StringBuilder();
		AlipayConfig ac = new AlipayConfig();
		ac.URLAuth = "https://openapi.alipay.com/gateway.do";
		ac.APPID = "xxxxx";
		ac.ALIPAY_PUBLIC_KEY = "xxxxx";
		// AlipayClient client = new
		// DefaultAlipayClient(ac.URLAuth,ac.APPID,ac.RSA_PRIVATE_KEY,ac.FORMAT,ac.CHARSET,ac.ALIPAY_PUBLIC_KEY,ac.SIGNTYPE);
		// try
		// {
		// AlipaySystemOauthTokenRequest request = new
		// AlipaySystemOauthTokenRequest();
		// request.setCode(Auth_Code);
		// request.setGrantType("authorization_code");
		// AlipaySystemOauthTokenResponse oauthTokenResponse =
		// client.execute(request);
		// // 获取令牌
		// strResult.append(oauthTokenResponse.getAccessToken());
		// /* 获取用户授权信息 */
		// AlipayUserInfoShareRequest Alirequest = new
		// AlipayUserInfoShareRequest();
		// AlipayUserInfoShareResponse userinfoShareResponse =
		// client.execute(Alirequest,strResult.toString());
		// strResult.delete(0,strResult.length());
		// strResult.append(userinfoShareResponse.getBody());
		// JSONObject json = null;
		// try
		// {
		// json = new JSONObject(strResult.toString());
		// if(null != json && !json.has("retcode"))
		// {
		// json = json.getJSONObject("alipay_user_info_share_response");
		// if(json.getString("msg").equals("Success"))
		// {
		// Ope_DB_Go pdg = new Ope_DB_Go();
		// String session_id = Com_Fun.Get_New_GUID();
		// String INF_EN_NAME = "A01_Ali_Login_OK";
		// String nick_name = "";
		// String user_id = "";
		// String avatar = "";
		// if(json.has("user_id"))
		// user_id = json.getString("user_id");
		// if(json.has("avatar"))
		// avatar = json.getString("avatar");
		// if(json.has("nick_name"))
		// nick_name = json.getString("nick_name");
		// else nick_name = user_id;
		// String[] Parameters = {nick_name,user_id,avatar};
		// String[] ParamTypes = {"STRING","STRING","STRING"};
		// String[] ParamOutName = new String[0];
		// String[] ParamOutType = new String[0];
		// String[] ReturnName =
		// {"s_result","error_desc","login_id","nickname","ALIPAY_ACCOUNT","M_ACCOUNT","E_M_NUMBER"};
		// String[] ReturnValue = {"","","","","","",""};
		// String[] ReturnType =
		// {"INT","STRING","STRING","STRING","STRING","STRING","INT"};
		// int[] ReturnISIMG = {0,0,0,0,0,0,0};
		// strResult.delete(0,strResult.length());
		// strResult.append(pdg.Ope_DB(session_id,INF_EN_NAME,Parameters,ParamTypes,ParamOutName,ParamOutType,ReturnName,ReturnValue,ReturnType,ReturnISIMG,ReturnISIMG));
		// }
		// }
		// }
		// catch(Exception ess)
		// {
		// strResult.delete(0,strResult.length());
		// strResult.append("{\"A01_Ali_Login_OK\":[{\"s_result\":\"0\",\"error_desc\":\"获取支付宝用户信息失败："
		// + ess.getMessage() + "\"}]}");
		// }
		// }
		// catch(AlipayApiException e)
		// {
		// strResult.delete(0,strResult.length());
		// strResult.append("{\"A01_Ali_Login_OK\":[{\"s_result\":\"0\",\"error_desc\":\"获取支付宝用户信息失败："
		// + e.getMessage() + "\"}]}");
		// com.zxy.adminlog.UsAdmin_Log uL = new
		// com.zxy.adminlog.UsAdmin_Log(Com_Para.ApplicationPath,new
		// StringBuilder("Ali_Login出错" + e.getMessage()));
		// try
		// {
		// uL.WriteLog();
		// }
		// catch(IOException e1)
		// {
		// }
		// }
		return strResult;
	}

	// 支付宝wap支付
	public String Pay_Now()
	{
		String strResult = "";
//		AlipayConfig ac = new AlipayConfig();
//		String timeout_express = "2m";
//		String product_code = "QUICK_WAP_PAY";
		/**********************/
		// SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
		// 调用RSA签名方式
		// @SuppressWarnings("static-access")
		// AlipayClient client = new
		// DefaultAlipayClient(ac.URL,ac.APPID,ac.RSA_PRIVATE_KEY,ac.FORMAT,ac.CHARSET,ac.ALIPAY_PUBLIC_KEY,ac.SIGNTYPE);
		// AlipayTradeWapPayRequest alipay_request = new
		// AlipayTradeWapPayRequest();
		// // 封装请求支付信息
		// AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
		// model.setOutTradeNo(WIDout_trade_no);
		// model.setSubject(WIDsubject);
		// model.setTotalAmount(WIDtotal_amount);
		// model.setBody(WIDbody);
		// model.setTimeoutExpress(timeout_express);
		// model.setProductCode(product_code);
		// alipay_request.setBizModel(model);
		// // 设置异步通知地址
		// alipay_request.setNotifyUrl(ac.notify_url);
		// // 设置同步地址
		// alipay_request.setReturnUrl(ac.return_url);
		// // form表单生产
		// try
		// {
		// strResult = client.pageExecute(alipay_request).getBody();
		// }
		// catch(AlipayApiException e)
		// {
		// strResult = e.getMessage();
		// StringBuilder sb = new StringBuilder();
		// sb.append("Pay_Now出错[" + strResult + "]\r\n");
		// com.zxy.adminlog.UsAdmin_Log uL = new
		// com.zxy.adminlog.UsAdmin_Log(Com_Para.ApplicationPath,sb);
		// try
		// {
		// uL.WriteLog();
		// }
		// catch(IOException e1)
		// {
		// }
		// }
		return strResult;
	}

	// 支付宝授权签名
	public String AuthInfo()
	{
		String strResult = "";
		AlipayConfig ac = new AlipayConfig();
		boolean rsa2 = (ac.RSA_PRIVATE_KEY.length() > 0);
		ac.APPID = "xxx";
		ac.PID = "xxx";
		Map<String, String> params = OrderInfoUtil.buildAuthSendParamMap(ac.APPID,rsa2,ac.PID,"001");
		String orderParam = OrderInfoUtil.buildOrderParam(params);
		String privateKey = ac.RSA_PRIVATE_KEY;
		String sign = OrderInfoUtil.getSign(params,privateKey,rsa2);
		strResult = "{\"Auth4Alipay\":[{\"AuthInfo\":\"" + orderParam + "&"
				+ sign + "\"}]}";
		return strResult;
	}

	// 支付宝订单签名，APP支付
	public String OrderInfo()
	{
		String strResult = "";
		AlipayConfig ac = new AlipayConfig();
		boolean rsa2 = (ac.RSA_PRIVATE_KEY.length() > 0);
		Map<String, String> params = OrderInfoUtil.buildOrderParamMap(ac.APPID,rsa2,WIDtotal_amount,WIDsubject,WIDbody,WIDout_trade_no);
		String orderParam = OrderInfoUtil.buildOrderParam(params);
		String privateKey = ac.RSA_PRIVATE_KEY;
		String sign = OrderInfoUtil.getSign(params,privateKey,rsa2);
		strResult = orderParam + "&" + sign;
		return "{\"A01_Order_Pay_Init\":[{\"OrderInfo\":\"" + strResult
				+ "\"}]}";
	}
}